package cn.gz.dzy.hm.sjjg.sort;

import java.util.Arrays;

/**
 * @program: java-interview
 * @description: 插入排序
 * @author: DingZhenYun
 * @create: 2021-11-09 18:44
 **/

public class InsertSort {
    public static void main(String[] args) {
        int[] a = {7, 5, 19, 8, 4, 1};
        insert(a);
    }

    private static void insert(int[] a) {
        // i 代表待插入元素的索引
        for (int i = 1; i <a.length ; i++) {
            int t = a[i]; // 代表待插入的元素值
            int j = i-1;
            System.out.println(j);
            while (j>=0){
                if(t<a[j]){
                    a[j+1]=a[j];
                }else{
                    break;
                }
                j--;
            }
            a[j+1] = t;
            System.out.println(Arrays.toString(a) + " " + j);


        }
    }
}
